package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.component.util.DateUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.trend.*;
import com.qumitech.visualnovel.pubdef.dto.trend.CommentAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.trend.TrendAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.amount.AmountQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.trend.TwitterTopCfgAdminDTO;
import com.qumitech.visualnovel.service.trend.TrendConfigService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Auther: yilu
 * @Date: 2018/12/6 23:04
 * @Description:
 */

@RestController
@RequestMapping("/api/trend")
public class TrendController {

    @Autowired
    private TrendConfigService trendConfigService;

    @Autowired
    DateUtil dateUtil;

    @GetMapping("/category")
    public BaseRsp getCategories(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return new BaseRsp(ResultEnum.SUCCESS, trendConfigService.getCategories(page, size));
    }

    @PostMapping("/category")
    public BaseRsp addCategory(@RequestBody TopicCategoryVO topicCategoryVO) {
        trendConfigService.addCategory(topicCategoryVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/category")
    public BaseRsp updateCategory(@RequestBody TopicCategoryVO topicCategoryVO) {
        trendConfigService.updateCategory(topicCategoryVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/category")
    public BaseRsp deleteCategory(@RequestParam("category_id") String cid) {
        trendConfigService.deleteCategory(cid);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/category/sort")
    public BaseRsp sortCategory(@RequestBody CategoryListVO categoryListVO) {
        trendConfigService.sortCategory(categoryListVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/topic")
    public BaseRsp getAllTopics() {
        return new BaseRsp(ResultEnum.SUCCESS, trendConfigService.getAllTopics());
    }

    @GetMapping("/topics")
    public BaseRsp listAllTopics() {
        return trendConfigService.listAllTopics(null);
    }

    @GetMapping("/topic/{category_id}")
    public BaseRsp getTopicByCategory(@PathVariable(name = "category_id") String categoryId) {
        return new BaseRsp(ResultEnum.SUCCESS, trendConfigService.getTopicsByCategory(categoryId));
    }

    @PostMapping("/topic")
    public BaseRsp addTopic(@RequestBody TopicVO topicVO) {
        trendConfigService.addTopic(topicVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/topic")
    public BaseRsp updateTopic(@RequestBody TopicVO topicVO) {
        trendConfigService.updateTopic(topicVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/topic")
    public BaseRsp deleteTopic(@RequestParam("topic_id") String tid) {
        trendConfigService.deleteTopic(tid);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/topic/sort")
    public BaseRsp sortTopics(@RequestBody TopicListVO topicListVO) {
        trendConfigService.sortTopics(topicListVO, false);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/hot_topic")
    public BaseRsp getHotTopics() {
        return new BaseRsp(ResultEnum.SUCCESS, trendConfigService.getAllHotTopics());
    }

    @PostMapping("/hot_topic")
    public BaseRsp addHotTopic(@RequestBody List<HotTopicVO> hotTopicVO) {
        trendConfigService.addHotTopic(hotTopicVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/hot_topic")
    public BaseRsp deleteHotTopic(@RequestParam("topic_id") String topicId) {
        trendConfigService.deleteHotTopic(topicId);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/hot_topic")
    public BaseRsp sortHotTopic(@RequestBody TopicListVO topicListVO) {
        trendConfigService.sortTopics(topicListVO, true);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping()
    public BaseRsp getTrend(@RequestParam(name = "limit", required = false, defaultValue = "10") String limit,
                            @RequestParam(name = "offset", required = false, defaultValue = "0") String offset,
                            @RequestParam(name = "trend_id", required = false) String trendId,
                            @RequestParam(name = "topic_id", required = false) String topicId,
                            @RequestParam(name = "user_id", required = false) String uid,
                            @RequestParam(name = "forbidden_state", required = false) String forbiddenState,
                            @RequestParam(name = "admin_state", required = false) String adminState,
                            @RequestParam(name = "startTime", required = false) String startTimeStamp,
                            @RequestParam(name = "endTime", required = false) String endTimeStamp,
                            @RequestParam(name = "trend_type", required = false) Integer trendType,
                            @RequestParam(name = "if_recommend", required = false) Boolean ifRecommend){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("limit", limit);
        paramMap.put("offset", offset);
        if(trendId != null){
            paramMap.put("trend_id", trendId);
        }
        if(topicId != null){
            paramMap.put("topic_id", topicId);
        }
        if(uid != null){
            paramMap.put("user_id", uid);
        }
        if(forbiddenState != null){
            paramMap.put("forbidden_state", forbiddenState);
        }
        if(adminState != null){
            paramMap.put("admin_state", adminState);
        }
        if(startTimeStamp != null){
            paramMap.put("start_time", String.valueOf(dateUtil.DateToTimestamp(startTimeStamp) * 1000));
        }
        if(endTimeStamp != null){
            paramMap.put("end_time", String.valueOf(dateUtil.DateToTimestamp(endTimeStamp) * 1000));
        }
        if(trendType != null){
            paramMap.put("trend_type", String.valueOf(trendType));
        }
        if(ifRecommend != null){
            paramMap.put("if_recommend", String.valueOf(ifRecommend));
        }
        return new BaseRsp(ResultEnum.SUCCESS, trendConfigService.getTrendByConditions(paramMap));
    }

    @PostMapping("/delete")
    public BaseRsp deleteTrend(@RequestBody List<String> trendId) {
        trendConfigService.deleteTrend(trendId);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/comment")
    public BaseRsp getComment(@RequestParam(name = "limit", required = false, defaultValue = "10") String limit,
                              @RequestParam(name = "offset", required = false, defaultValue = "0") String offset,
                              @RequestParam(name = "trend_id", required = false) String trendId,
                              @RequestParam(name = "content", required = false) String content,
                              @RequestParam(name = "user_id", required = false) String fromUid,
                              @RequestParam(name = "admin_state", required = false, defaultValue = "0") String adminState,
                              @RequestParam(name = "startTime", required = false) String startTimeStamp,
                              @RequestParam(name = "endTime", required = false) String endTimeStamp){
        Map<String, String> paramMap = new HashMap<>(16);
        paramMap.put("limit", limit);
        paramMap.put("offset", offset);

        if(trendId != null){
            paramMap.put("trend_id", trendId);
        }
        if(content != null){
            paramMap.put("content", content);
        }
        if(fromUid != null){
            paramMap.put("user_id", fromUid);
        }
        if(adminState != null){
            paramMap.put("admin_state", adminState);
        }
        if(startTimeStamp != null){
            paramMap.put("start_time", String.valueOf(dateUtil.DateToTimestamp(startTimeStamp) * 1000));
        }
        if(endTimeStamp != null){
            paramMap.put("end_time", String.valueOf(dateUtil.DateToTimestamp(endTimeStamp) * 1000));
        }
        return new BaseRsp(ResultEnum.SUCCESS, trendConfigService.getComments(paramMap));
    }

    @PostMapping("/comment")
    public BaseRsp addComment(@RequestBody CommentDTO dto){
        trendConfigService.addComment(dto);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/comment")
    public BaseRsp deleteComment(@RequestParam("comment_id") String commentId){
        trendConfigService.deleteComment(commentId);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/admin")
    public BaseRsp updateTrendAdmin(@RequestBody TrendAdminDTO trendAdminDTO) {
        trendConfigService.updateTrendAdmin(trendAdminDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/comment/admin")
    public BaseRsp updateCommentAdmin(@RequestBody CommentAdminDTO commentAdminDTO) {
        trendConfigService.updateCommentAdmin(commentAdminDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/trend/num")
    public BaseRsp getUserTrendNum(AmountQueryDTO amountQueryDTO){
        Map<String, String> paramMap = constructNumParamMap(amountQueryDTO);
        return new BaseRsp(ResultEnum.SUCCESS, trendConfigService.getUserTrendSum(paramMap));
    }

    @GetMapping("/comment/num")
    public BaseRsp getUserCommentNum(AmountQueryDTO amountQueryDTO){
        Map<String, String> paramMap = constructNumParamMap(amountQueryDTO);
        return new BaseRsp(ResultEnum.SUCCESS, trendConfigService.getUserCommentSum(paramMap));
    }

    @GetMapping("/like/num")
    public BaseRsp getUserLikeNum(AmountQueryDTO amountQueryDTO){
        Map<String, String> paramMap = constructNumParamMap(amountQueryDTO);
        return new BaseRsp(ResultEnum.SUCCESS, trendConfigService.getUserLikeSum(paramMap));
    }

    @GetMapping("/top")
    public BaseRsp listTwitterTopCfg(@RequestParam(name = "limit", required = false, defaultValue = "10") String limit,
                                     @RequestParam(name = "offset", required = false, defaultValue = "0") String offset,
                                     @RequestParam(name = "twitter_id", required = false) String twitterId){
        Map<String, String> paramMap = new HashMap<>(16);
        paramMap.put("limit", limit);
        paramMap.put("offset", offset);

        if(twitterId != null){
            paramMap.put("twitter_id", twitterId);
        }
        return trendConfigService.listAllTopTwitter(paramMap);
    }

    @PutMapping("/top")
    public BaseRsp addTwitterTopCfg(@RequestBody TwitterTopCfgAdminDTO dto){
        return trendConfigService.saveTopTwitter(dto);
    }


    @DeleteMapping("/top/{id}")
    public BaseRsp deleteTwitterTopCfg(@PathVariable("id") Long id){
        return trendConfigService.deleteTopTwitter(id);
    }

    private Map<String, String> constructNumParamMap(AmountQueryDTO amountQueryDTO){
        AmountQueryDTO dto = new AmountQueryDTO(amountQueryDTO.getUid(), amountQueryDTO.getStartTime(), amountQueryDTO.getEndTime());
        Map<String, String> paramMap = new HashMap<>();
        if (dto.getUid() != null){
            paramMap.put("user_id", amountQueryDTO.getUid());
        }
        if (dto.getStartTime() != null){
            paramMap.put("start_time", amountQueryDTO.getStartTime());
        }
        if (dto.getEndTime() != null){
            paramMap.put("end_time", amountQueryDTO.getEndTime());
        }
        return paramMap;
    }
}
